package com.example.yangyuhua

import com.example.yangyuhua.model.Student
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.ComponentActivity

class AddStudentActivity : ComponentActivity() {

    private lateinit var editTextId: EditText
    private lateinit var editTextName: EditText
    private lateinit var editTextGender: EditText
    private lateinit var editTextClass: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_student)

        editTextId = findViewById(R.id.editTextId)
        editTextName = findViewById(R.id.editTextName)
        editTextGender = findViewById(R.id.editTextGender)
        editTextClass = findViewById(R.id.editTextClass)

        val buttonConfirm: Button = findViewById(R.id.buttonConfirm)
        buttonConfirm.setOnClickListener {
            val studentId = editTextId.text.toString()
            val name = editTextName.text.toString()
            val gender = editTextGender.text.toString()
            val className = editTextClass.text.toString()

            val student = Student(studentId, name, gender, className)
            val resultIntent = Intent()
            resultIntent.putExtra(MainActivity.EXTRA_STUDENT, student)
            setResult(RESULT_OK, resultIntent)
            finish()
        }

        val buttonCancel: Button = findViewById(R.id.buttonCancel)
        buttonCancel.setOnClickListener {
            setResult(RESULT_CANCELED)
            finish()
        }
    }
}
